var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "ofid",
			name : "id",
			hide : true
		}, {
			colkey : "ofcode",
			name : "订单编号",
		}, {
			colkey : "empName",
			name : "编制人",
			isSort:true,
		}, {
			colkey : "num",
			name : "订单数量",
			isSort:true,
		}, {
			colkey : "billStatusName",
			name : "状态"
		}, {
			colkey : "workshopName",
			name : "车间",
		}, {
			colkey : "stockname",
			name : "成品名称"
		}, {
			colkey : "btime",
			name : "订购日期"
		},{
			colkey : "etime",
			name : "要求日期"
		}
		],
		jsonUrl : rootPath + '/order/getOrderByPage.shtml',
		dymCol:true,
		checkbox : true,
		checkValue: "ofid",
		records: "list",
		totalPages: "pages",
		totalRecords: "total",
		pagecode: '10',
		pageNow : 'pageNum'
	});
	$("#search").click("click", function() {
		var sel = $("#billstateid option:selected").val();//获取下拉框数据
		var myv = $("#btime").val();//获取输入框数据
		//放出到input框中
		$("#myValue").val(sel+","+myv);
		var searchParams = $("#myValue").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editAccount").click("click", function() {
		editAccount();
	});
	$("#delAccount").click("click", function() {
		delAccount();
	});
	$("#view").click("click", function() {
		view();
	});


});
function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "850px", "80%" ],
		content : rootPath + '/order/editOrderUI.shtml?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "900px", "80%" ],
		content : rootPath + '/order/openOrderManagementUI.shtml'
	});
}
function delAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/order/deleteOrder.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
function view() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择要查看产品");
		return;
	}
	pageii = layer.open({
		title : "物料需求",
		type : 2,
		area : [ "600px", "66%" ],
		content : rootPath + '/order/openMaterial.shtml?id=' + cbox
	});
}

//导出
function exportExcel(excelName){
    // var mytable = "";//需要导出的数据
    var fileName = excelName+".xlsx"

    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {//没选则导出所有数据
        var mytable = document.getElementById("mytable");//获取table
        var sheet = XLSX.utils.table_to_sheet(mytable);
    } else {//第二种：导出指定的行

        //获取表头标题，就是表中第一行，序号不存在
        var valueTd= document.getElementById ("mytable").rows[0];

        //复制节点
        var clone_valueTd = valueTd.cloneNode(true);
        // debugger;

        //根据选中行，动态拼接table
        // var tableData = "<table><tr>" + valueTd.innerHTML + "</tr>";
        var table = document.createElement('table');
        table.id = "table1";
        table.appendChild(clone_valueTd);//使用原有节点会对原有dom造成影响

        //获取所有表中所有checkbox
        var inputs = document.getElementById("mytable").getElementsByTagName("input");
        console.log("inputs.length:"+inputs.length);
        for(var i=0;i<inputs.length;i++) {//获取选中行
            if (inputs[i].type == "checkbox") {
                if (inputs[i].checked) {
                    var checkedRow = inputs[i];
                    var tr = checkedRow.parentNode.parentNode;
                    var clone_tr = tr.cloneNode(true);//复制
                    console.log(tr.cells[0].innerText);
                    if(tr.cells[0].innerText != ""){//序号不为空则拼接，为空说明是表头标题
                        // tableData += "<tr>" + tr.innerHTML + "</tr>";
                        table.appendChild(clone_tr)
                    }
                    // console.log("为空是表头，不拼接")
                    // console.log("tr...:"+tr);
                }
            }
        }

        // tableData += "</table>";
        // mytable = document.createElement(tableData);
        var sheet = XLSX.utils.table_to_sheet(table);
    }

    openDownloadDialog(sheet2blob(sheet), fileName);
}

//将订单编号生成二维码导出
function exportQRExcel(){
    var cbox = grid.getSelectedCheckbox();

    console.log('cbox:'+cbox);

    var url = "";
    if (cbox == "") {//没选则导出所有数据
        // window.location.href = rootPath + "/order/qrData.shtml";
        url = rootPath + "/order/qrData.shtml";
    } else {//第二种：导出指定的行
        // window.location.href = rootPath + "/order/qrData.shtml?id=" + cbox;
        url = rootPath + "/order/qrData.shtml?id=" + cbox;
    }

		//请求后台打印数据,ajax请求只能返回数据类型，文件下载类不能使用ajax,需要使用window.location.href = url的方式
    $.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        success: function(data) {
        	console.log(data);
            var someJSONdata = data;
            var divNode = document.getElementById("divNode");
            // var divNode = document.createElement("div");
            // divNode.setAttribute("id","divNode")
            var tb1 = '<table  border="1" style="border-collapse: collapse;width: 80%;margin: auto">' +
                		<!-- 标题 -->
                		'<caption>订单管理</caption>'+
                		<!-- 头部 -->
						'<thead>'+
							'<th>订单编号</th>'+
							'<th>二维码</th>'+
							'<th>成品</th>'+
							'<th>订单数量</th>'+
							'<th>编制人</th>'+
                			'<th>车间</th>'+
							'<th>订单状态</th>'+
						'</thead>'+
						'<tbody>'
            for(var i=0;i<data.length;i++){
				tb1 += '<tr>'+
						'<td  style="text-align: center;">' + data[i].订单编号 + '</td>'+
                    	'<td  style="text-align: center;"><img src="'+ data[i].二维码 +'" style="width: 100px;height: 100px"/></td>'+
						'<td  style="text-align: center;">' + data[i].成品 + '</td>'+
						'<td  style="text-align: center;">' + data[i].订单数量 + '</td>'+
						'<td  style="text-align: center;">' + data[i].编制人 + '</td>'+
                    	'<td  style="text-align: center;">' + data[i].车间 + '</td>'+
						'<td  style="text-align: center;">' + data[i].订单状态 + '</td>'+
						'</tr>'

			}
            tb1 += '</tbody>'+'</table>';
            divNode.innerHTML = tb1;
            console.log(divNode);

                printJS({
				printable: 'divNode',
				// properties: ['订单编号', '二维码', '成品', '订单数量', '编制人', '订单状态'],
				type: 'html',
                    // gridHeaderStyle: 'color: red;  border: 2px solid #3971A5;',
                    // gridStyle: 'border: 2px solid #3971A5;',
                    scanStyles: false,
                    targetStyles: ['*']
                })
        }
    });

}